package com.cdyx.outsideprescription.util;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.models.auth.In;
import lombok.Data;

import java.util.List;

/**
 * 自定义响应结构
 */
@Data
public class Result {
    private static  final ObjectMapper MAPPER=new ObjectMapper();
    //影响业务状态
    private Integer status;
    //响应业务消息
    private String msg;
    //响应中的数据
    private Object data;
    public Result(){
    }

    public Result(Integer status, String msg, Object data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public Result(Object data) {
        this.data = data;
        this.msg="OK";
        this.status=200;
    }
    public Result(Integer status,String msg){
        this.msg=msg;
        this.status=status;
    }
    public static Result build(Integer status, String msg, Object data){
        return new Result(status,msg,data);
    }

    public static Result ok(Object data){
        return new Result(data);
    }

    public  static Result ok(){
        return  new Result(null);
    }
    public  static Result fail(Integer status,String msg){
        return  new Result(status,msg);
    }

public static Result build(Integer status,String msg){
        return new Result(status,msg,null);
}


    /**
     * 将json结果集转化为Result
     * @param jsonData
     * @param clazz
     * @return
     */
    public static Result formatToPojo(String jsonData,Class<?> clazz){
        try {
            if (clazz == null) {
                return MAPPER.readValue(jsonData, Result.class);
            }
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (clazz != null) {
                if (data.isObject()) {
                    obj = MAPPER.readValue(data.traverse(), clazz);
                } else if (data.isTextual()) {
                    obj = MAPPER.readValue(data.asText(), clazz);
                }
            }
            return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 没有Object对象的转换
     * @param json
     * @return
     */
    public static Result format(String json) {
        try {
            return MAPPER.readValue(json, Result.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * object是集合的转换
     */
    public static Result formatToList(String jsonData, Class<?> clazz) {
        try {
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (data.isArray() && data.size() > 0) {
                obj = MAPPER.readValue(data.traverse(),
                        MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
            }
            return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }

}
